//
//  GRSafetyCertificateViewController.m
//  YC_GRice
//
//  Created by LiuFeng on 2018/7/21.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRSafetyCertificateViewController.h"
#import "GRPersonalCertificationViewController.h"
#import "GRIDCardCertificationViewController.h"
#import "GRContactCertificationViewController.h"
#import "GRContactInfomationViewController.h"

#import "GRSafetyCertificationViewCell.h"
#import "GRCertificationHeader.h"

@interface GRSafetyCertificateViewController ()

{
    NSArray *resultKeys;
}
@property (nonatomic, strong) NSDictionary *resultDic;

@end

@implementation GRSafetyCertificateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    resultKeys = @[@"personal", @"contacter"];
    // Uncomment the following line to preserve selection between presentations.
    GRCertificationHeader *header  = [[GRCertificationHeader alloc] initWithCertification:GRCertificationSafety];
    self.tableView.tableHeaderView = header;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.rowHeight       = 50;
    self.tableView.backgroundColor = HEXCOLOR(0xf8f8f8);
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    [self.tableView registerNib:[UINib nibWithNibName:@"GRSafetyCertificationViewCell" bundle:nil] forCellReuseIdentifier:@"GRSafetyCertificationViewCell"];
    
    [self getCertification];
}

- (void)getCertification {
    [GRCertificationAction getALLCertificationStatus:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            self.resultDic = responseObject;
            [self.tableView reloadData];
        }else {
            [GRHUD showMessage:[error errorMsg] atView:self.view];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GRSafetyCertificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GRSafetyCertificationViewCell" forIndexPath:indexPath];
    
    [cell safetyConfigureAtIndexPath:indexPath];
    
    NSString *key = resultKeys[indexPath.row];
    if (self.resultDic[key]) {
        cell.safetyStatus.text = [self.resultDic[key] boolValue] ? @"已认证" : @"未认证";
    }
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = resultKeys[indexPath.row];
    switch (indexPath.row) {
        case 0:{
            if ([self.resultDic[key] boolValue]) {
                GRPersonalCertificationViewController *personalVc = [[GRPersonalCertificationViewController alloc] initWithStyle:UITableViewStylePlain];
                [self.navigationController pushViewController:personalVc animated:YES];
            }else {
                GRIDCardCertificationViewController *idcardVc = [[GRIDCardCertificationViewController alloc] initWithStyle:UITableViewStylePlain];
                [self.navigationController pushViewController:idcardVc animated:YES];
            }
            
        }
            break;
            
        default:{
            if ([self.resultDic[key] boolValue]) {
                GRContactInfomationViewController *infoVc = [[GRContactInfomationViewController alloc] initWithStyle:UITableViewStylePlain];
                [self.navigationController pushViewController:infoVc animated:YES];
            }else {
                GRContactCertificationViewController *contactVc = [[GRContactCertificationViewController alloc] initWithStyle:UITableViewStyleGrouped];
                WEAKSELF
                contactVc.refreshBlock = ^{
                    [weakSelf getCertification];
                    if (weakSelf.certifiedBlock) {
                        weakSelf.certifiedBlock();
                    }
                };
                [self.navigationController pushViewController:contactVc animated:YES];
            }
        }
            break;
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
